﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;

namespace AWA.VAT.DbAccess.SQL
{
    public class EntryComparer : IComparer<Entry>
    {
        public int Compare(Entry x, Entry y)
        {
            // sort by year
            if (x.Year != y.Year)
                return x.Year - y.Year;

            // then sort by contest
            if (x.ContestID != y.ContestID)
                return x.ContestID - y.ContestID;

            // then sort by award
            StringComparer comparer = StringComparer.Create(CultureInfo.CurrentCulture, true);
            if (x.Awards.Count == 0 & y.Awards.Count == 0)
            {
                // then sort by title
                return comparer.Compare(x.AMV.AMVTitle, y.AMV.AMVTitle);
            }

            // list those with awards first
            if (x.Awards.Count == 0)
                return 1;
            if (y.Awards.Count == 0)
                return -1;

            // sort by first award alphabetically
            int result = comparer.Compare(x.Awards.ElementAt(0).AwardName, y.Awards.ElementAt(0).AwardName);
            if (result != 0)
                return result;

            // sort by title
            return comparer.Compare(x.AMV.AMVTitle, y.AMV.AMVTitle);
        }
    }
}
